package cn.graydove.bot.listener;

import cn.graydove.bot.adapter.GroupMessageEvent;
import cn.graydove.bot.adapter.RobotClient;
import cn.graydove.bot.annotation.Listener;
import cn.graydove.bot.core.Assert;
import cn.graydove.bot.service.AdvanceService;
import org.springframework.stereotype.Component;

@Component
public class VIPListener {

    private Assert asserts;

    private AdvanceService advanceService;

    public VIPListener(Assert asserts, AdvanceService advanceService) {
        this.asserts = asserts;
        this.advanceService = advanceService;
    }

    @Listener("^激活.*")
    public void activate(GroupMessageEvent event, RobotClient client) {
        asserts.groupAdminReceive(event, group -> {
            try {
                String code = event.getMessage().substring("激活".length()).trim();
                if (advanceService.isActivated(event.getGroupId())) {
                    client.sendGroupMsg(event.getGroupId(), "请勿重复激活");
                    return;
                }
                if (advanceService.activate(event.getGroupId(), code)) {
                    client.sendGroupMsg(event.getGroupId(), "激活成功");
                } else {
                    client.sendGroupMsg(event.getGroupId(), "无效激活码");
                }
            } catch (Exception e) {
                client.sendGroupMsg(event.getGroupId(), "无效激活码");
            }
        });
    }
}
